package parkvilla.tv

import android.os.Bundle
import android.view.KeyEvent
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.C
import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.AspectRatioFrameLayout
import androidx.media3.ui.PlayerView
import androidx.tv.material3.ExperimentalTvMaterial3Api
import androidx.tv.material3.Surface
import parkvilla.tv.ui.theme.TVTheme

val sources =
    arrayOf(
        Pair(
            "东方卫视",
            "https://epg.pw/stream/eb7a5a450a87157712e9afc63da8f145ee437bbe67c89afe80ae08611c46c61d.m3u8"
        ),
        Pair(
            "湖南卫视",
            "https://epg.pw/stream/98760919199aeb391854e9698f0ec090972c1f03879d4fd98943994d7812205d.m3u8"
        ),
        Pair(
            "浙江卫视",
            "http://ottrrs.hl.chinamobile.com/PLTV/88888888/224/3221225612/index.m3u8"
        )
    )

class MainActivity : ComponentActivity() {

    var exoPlayer: ExoPlayer? = null

    @androidx.annotation.OptIn(UnstableApi::class)
    @OptIn(ExperimentalTvMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        exoPlayer = ExoPlayer.Builder(this).build().apply {
            playWhenReady = false
            videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
        }

        setContent {
            TVTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize()
                ) {
                    AndroidView(factory = { context ->
                        PlayerView(context).apply {
                            useController = false
                            player = exoPlayer
                            resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL
                        }
                    }, modifier = Modifier.fillMaxSize())
                }
            }
        }

        play()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        when (keyCode) {
            KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_VOLUME_UP -> {
                exoPlayer?.seekToPreviousMediaItem()
            }

            KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_VOLUME_DOWN -> {
                exoPlayer?.seekToNextMediaItem()
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun play() {
        exoPlayer?.apply {
            sources.forEach { source ->
                val mediaItem = MediaItem.fromUri(source.second)
                addMediaItem(mediaItem)
            }
            prepare()
            play()
        }
    }
}